package com.qinghe.pro.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.qinghe.utils.Page;

public class CopyOfPageTag extends SimpleTagSupport {

	private Page page;

	private int pageRange = 10;

	@Override
	public void doTag() throws JspException, IOException {

		StringBuilder builder = new StringBuilder();
		createHtml(builder);
		createJs(builder);

		getJspContext().getOut().write(builder.toString());
	}

    private void createHtml(StringBuilder builder) {
		long begin = Math.max(1, page.getPageNum() - pageRange/2);
	    long end = Math.min(begin + (pageRange - 1), page.getTotalPage());

		builder.append("<div class=\"page\">\n");
		builder.append("<div class=\"more\">\n");
		builder.append("共&nbsp;" + page.getTotalPage() + "&nbsp;页，");
		builder.append("到第<input type=\"text\" id=\"p_pageIndex\" value=\"" + page.getPageNum() + "\" class=\"inta\" />页&nbsp;");
		builder.append("<input type=\"button\" value=\"确定\" onclick=\"gotoPageByBtn()\" class=\"jbtn\" />\n");
		builder.append("</div>\n");

		builder.append("<div class=\"box\">\n");
		builder.append("<dl>\n");
	    if (page.getPageNum() != 1) {
			builder.append("<dd class=\"col\" onclick=\"gotoPage(" + (page.getPageNum() - 1) + ")\">< 上一页</dd>\n");
	    } else {
			builder.append("<dd class=\"grey\">< 上一页</dd>\n");
	    }
	    for (long i = begin; i <= end; i++) {
	    	if (i == page.getPageNum()) {
	    		builder.append("<dd class=\"on\">" + i + "</dd>\n");
	    	} else {
	    		builder.append("<dd onclick=\"gotoPage(" + i + ")\">" + i + "</dd>\n");
	    	}
	    }
	    if (page.getPageNum() == page.getTotalPage()) {
    		builder.append("<dd class=\"grey\">下一页 ></dd>\n");
	    } else {
    		builder.append("<dd class=\"col\" onclick=\"gotoPage(" + (page.getPageNum() + 1) + ")\">下一页 ></dd>\n");
	    }
		builder.append("</dl>\n");
		builder.append("</div>\n");
		builder.append("</div>\n");
    }

    private void createJs(StringBuilder builder) {
    	builder.append("<script type=\"text/javascript\">");
    	builder.append("function gotoPage(pageNum) {");
    	builder.append("    var form = document.getElementById(\"p_pagerForm\");");
    	builder.append("    document.getElementById(\"p_pageNum\").value = pageNum;");
    	builder.append("    form.submit();");
    	builder.append("}");

    	builder.append("function gotoPageByBtn() {");
    	builder.append("    var pageIndex = document.getElementById(\"p_pageIndex\");");
    	builder.append("    var pageIndexInt = parseInt(pageIndex.value);");
    	builder.append("    var totalPage = " + page.getTotalPage() + ";");
    	builder.append("    if (pageIndexInt > 0 && pageIndexInt <= totalPage) {");
    	builder.append("        gotoPage(pageIndexInt);");
    	builder.append("    } else {");
    	builder.append("        pageIndex.select();");
    	builder.append("    }");
    	builder.append("}");
    	builder.append("</script>");
    }

	public void setPage(Page page) {
		this.page = page;
	}

	public void setPageRange(int pageRange) {
		this.pageRange = pageRange;
	}

}
